package rexsee.core.widget;

import rexsee.core.animation.AnimationStyleSheet;
import rexsee.core.browser.Browser;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.style.StyleSheet;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Div extends FrameLayout {

	private String mId;
	protected final Context mContext;
	public final LinearLayout mVerticalLayout;
	public final LinearLayout mHorizontalLayout;
	public final ImageView mLeftBorder;
	public final ImageView mTopBorder;
	public final ImageView mRightBorder;
	public final ImageView mBottomBorder;
	public final FrameLayout innerLayout;
	public final FrameLayout coverLayout;

	public StyleSheet styleSheet;

	public Div(Context context) {

		super(context);
		mContext = context;
		mVerticalLayout = new LinearLayout(context);
		mHorizontalLayout = new LinearLayout(context);
		mLeftBorder = new ImageView(context);
		mTopBorder = new ImageView(context);
		mRightBorder = new ImageView(context);
		mBottomBorder = new ImageView(context);
		innerLayout = new FrameLayout(context);
		coverLayout = new FrameLayout(context);

		this.setBackgroundColor(Color.TRANSPARENT);
		/*
		mHorizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
		mHorizontalLayout.addView(mLeftBorder, new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.FILL_PARENT));
		mHorizontalLayout.addView(mInnerLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 2));
		mHorizontalLayout.addView(mRightBorder, new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.FILL_PARENT));
		mVerticalLayout.setOrientation(LinearLayout.VERTICAL);
		mVerticalLayout.addView(mTopBorder, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 1));
		mVerticalLayout.addView(mHorizontalLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 2));
		mVerticalLayout.addView(mBottomBorder, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 1));
		this.addView(mVerticalLayout);
		*/
		mVerticalLayout.setOrientation(LinearLayout.VERTICAL);
		mVerticalLayout.addView(mTopBorder, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 1));
		mVerticalLayout.addView(innerLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 2));
		mVerticalLayout.addView(mBottomBorder, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 1));
		mHorizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
		mHorizontalLayout.addView(mLeftBorder, new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.FILL_PARENT));
		mHorizontalLayout.addView(mVerticalLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 2));
		mHorizontalLayout.addView(mRightBorder, new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.FILL_PARENT));
		this.addView(mHorizontalLayout);
		coverLayout.setVisibility(GONE);
		this.addView(coverLayout);

		styleSheet = new StyleSheet(context);
		setDivStyle();

	}

	public final void setDivId(String id) {
		mId = id;
	}
	public final String getDivId() {
		return mId;
	}

	public final void setDivStyleWithoutLayoutParams() {

		try {

			Drawable bgDrawable = RexseeDrawable.getDrawable(mContext, null, styleSheet.background_color);
			setDivBackgroundDrawable(bgDrawable);

			mHorizontalLayout.setVisibility((styleSheet.inner_visibility.equalsIgnoreCase("hidden")) ? GONE : VISIBLE);
			this.setVisibility((styleSheet.visibility.equalsIgnoreCase("hidden")) ? GONE : VISIBLE);
			this.setPadding(styleSheet.getMarginLeft(), styleSheet.getMarginTop(), styleSheet.getMarginRight(), styleSheet.getMarginBottom());
			this.setSoundEffectsEnabled(styleSheet.play_sound_effect.equals("true"));
			innerLayout.setPadding(styleSheet.getPaddingLeft(), styleSheet.getPaddingTop(), styleSheet.getPaddingRight(), styleSheet.getPaddingBottom());

			mLeftBorder.setLayoutParams(new LinearLayout.LayoutParams(styleSheet.getBorderWidthLeft(), LinearLayout.LayoutParams.FILL_PARENT));
			mLeftBorder.setAlpha(StyleSheet.getImageAlpha(styleSheet.border_left_alpha));
			mLeftBorder.setScaleType(StyleSheet.getScaleType(styleSheet.border_left_scale));
			mLeftBorder.setImageDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.border_left_color));

			mTopBorder.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, styleSheet.getBorderWidthTop()));
			mTopBorder.setAlpha(StyleSheet.getImageAlpha(styleSheet.border_top_alpha));
			mTopBorder.setScaleType(StyleSheet.getScaleType(styleSheet.border_top_scale));
			mTopBorder.setImageDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.border_top_color));

			mRightBorder.setLayoutParams(new LinearLayout.LayoutParams(styleSheet.getBorderWidthRight(), LinearLayout.LayoutParams.FILL_PARENT));
			mRightBorder.setAlpha(StyleSheet.getImageAlpha(styleSheet.border_right_alpha));
			mRightBorder.setScaleType(StyleSheet.getScaleType(styleSheet.border_right_scale));
			mRightBorder.setImageDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.border_right_color));

			mBottomBorder.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, styleSheet.getBorderWidthBottom()));
			mBottomBorder.setAlpha(StyleSheet.getImageAlpha(styleSheet.border_bottom_alpha));
			mBottomBorder.setScaleType(StyleSheet.getScaleType(styleSheet.border_bottom_scale));
			mBottomBorder.setImageDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.border_bottom_color));

		} catch (Exception e) {
			Toast.makeText(mContext, "Div:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}

	}

	public void setDivBackgroundDrawable(Drawable bgDrawable) {
		innerLayout.setBackgroundDrawable(bgDrawable);
		if (styleSheet.margin_background.equalsIgnoreCase("sameasforeground")) {
			this.setBackgroundDrawable(bgDrawable);
		} else {
			this.setBackgroundDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.margin_background));
		}
	}

	public final void setDivStyle() {
		if (this.getParent() == null) this.setLayoutParams(styleSheet.getLayoutParams());
		setDivStyleWithoutLayoutParams();
	}
	public final void setDivStyle(String style) {
		styleSheet.parseStyle(style);
		setDivStyle();
	}
	public final String getDivStyle(String name) {
		if (name.equalsIgnoreCase("width")) {
			return String.valueOf(getWidth());
		} else if (name.equalsIgnoreCase("height")) {
			return String.valueOf(getHeight());
		} else {
			String value = styleSheet.getStyle(name);
			return (value == null) ? "null" : value;
		}
	}
	public final void addDivChild(View child) {
		innerLayout.addView(child, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
	}
	public final void removeDivChilds() {
		innerLayout.removeAllViews();
	}

	public void setStyleWithoutLayoutParams(String style) {
		styleSheet.parseStyle(style);
		setDivStyleWithoutLayoutParams();
	}
	public void setStyle(String style) {
		setDivStyle(style);
	}
	public String getStyle(String name) {
		return getDivStyle(name);
	}
	public StyleSheet getStyleSheet() {
		return styleSheet;
	}

	public void addChild(View child) {
		addDivChild(child);
	}
	public void removeChilds() {
		removeDivChilds();
	}

	public void animate(Browser browser, String style) {
		AnimationStyleSheet styleSheet = new AnimationStyleSheet(browser).parseStyle(style + ";id:" + mId + ";");
		styleSheet.animate(mHorizontalLayout);
	}
	public void stopAnimate() {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mHorizontalLayout.clearAnimation();
			}
		});
	}

	public void setCoverToTop(boolean top) {
		if (top) {
			coverLayout.bringToFront();
		} else {
			mHorizontalLayout.bringToFront();
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		try {
			super.onDraw(canvas);
		} catch (Exception e) {
			Toast.makeText(mContext, "Div.onDraw:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
	}

}
